# -*- coding: utf-8 -*-

from dbobject import DBObject
from reference import Reference
import compound

import yaml


class Reaction(DBObject):
	"""This class holds the information of a chemical
	reaction. It holds info like temp catalysts...
	Also it has a list of catalysts kept in a different
	table.
	This class will auto populate itself
	"""

	def __init__(self, reactionKey=''):

		DBObject.__init__(self)

		self.key = reactionKey

		#List of catalyst object
		self._things['catalysts'] = []

		#List of equCompound object
		self._things['reactants'] = []
		self._things['products'] = []

		#Reference object
		self._things['reference'] = None

		#Integers
		self._things['temperature'] = None
		self._things['pressure'] = None
		self._things['time'] = None
		self._things['pH'] = None

		#Strings
		self._things['solvent'] = ''
		self._things['other'] = ''

		#bools
		self._things['liquid'] = False
		self._things['gas'] = False
		self._things['solid'] = False

	def _loadYAML(self, yml):

		data = yaml.load(yml)
		
		if "catalysts" in data.keys():
			keys = data["catalysts"]
			if keys:
				self._things["catalysts"] = [Catalyst(i) for i in keys]
				del data["catalysts"]
		if "products" in data.keys():
			d = data["products"]
			if d:
				self._things["products"] = [compound.EquCompound(key, d[key]) for key in d]
				print self._things["products"]
				del data["products"]
		if "reactants" in data.keys():
			d = data["reactants"]
			if d:
				self._things["reactants"] = [compound.EquCompound(key, d[key]) for key in d]
				print self._things["products"]
				del data["reactants"]
				print self._things["reactants"]
		if "reference" in data.keys():
			if data["reference"]:
				self._things["reference"] = Reference(data["reference"])
				del data["reference"]

		DBObject._loadYAML(self, data)

	def _dumpYAML(self, properties=[]):
		print "Trying to export a reaction"
		
		if properties:
			keys = properties
		else:
			keys = self.properties().keys()

		data = {}

		print keys
		
		if "catalysts" in keys:
			keys.remove("catalysts")
			data["catalysts"] = [i.key for i in self.catalysts]
		if "products" in keys:
			keys.remove("products")
			data["products"] = {}
			for i in self.products:
				print i, i.key, i._loaded
				data["products"][i.key] = i._things['percent']
		if "reactants" in keys:
			keys.remove("reactants")
			data["reactants"] = {}
			for i in self.reactants:
				data["reactants"][i.key] = i._things['percent']

		rest = list(set(keys) - set(data.keys()))
		print rest
		print "Aux data: ", data
		return DBObject._dumpYAML(self, otherData=data, properties=rest)
		


class Catalyst(DBObject):
	"""This class holds the information of a chemical
	reaction. It holds info like temp catalysts...
	Also it has a list of catalysts kept in a different
	table.
	This class will auto populate itself
	"""

	def __init__(self, catalystKey=''):

		DBObject.__init__(self)

		self.key = catalystKey

		#string
		self._things['name'] = ""

		#reaction object
		self._things['reaction'] = None